import type { ProFormInstance } from '@ant-design/pro-form';
import React, { useRef, useState } from 'react';
import { Button, Popconfirm,  } from 'antd';
import { ActionType, ProColumns, ProTable,DrawerForm } from '@ant-design/pro-components';
import { PlusOutlined } from '@ant-design/icons';
import * as api from '@/services/geoip';
import FormAreaForm from './FormAreaForm';
import FlagImage from '@/components/FlagImage';

type FormProps = {
  open: boolean;
  onCancel: () => void;
};

const Form: React.FC<FormProps> = (props) => {
  const { open, onCancel } = props;
  if (!open) {
    return null;
  }
  
  const formRef = useRef<ProFormInstance>();
  const actionRef = useRef<ActionType>();
  const [current, setCurrent] = useState<API.Geoip | undefined>(undefined);

  const [formVisible, setFormVisible] = useState<boolean>(false);

  

 

  const handleSubmit = async (record: API.Geoip) => {
    let result: API.Result;
    if (current) {
      result = await api.update({
        ...current,
        ...record,
      });
    } else {
      result = await api.add({ ...record });
    }

    if (result && result.success === true) {
      setCurrent(undefined);
      setFormVisible(false);
      actionRef.current?.reload();
    }

  };
  
  const handleRemove = async (id: string) => {
    let result = await api.remove({key:id});
    if (result && result.success === true) {
      actionRef.current?.reload();
    }
  };
  
  

  
  let columns: ProColumns<API.Geoip>[] = [
    {
      title: '名称',
      dataIndex: 'name',
      readonly: true,
      width:200,
      render: (_, record) => {
        return <><FlagImage flagName={record.flag} />{record.name}</>;
      },
    },
    {
      title: '备注',
      dataIndex: 'remark',
      readonly: true,
    },

  ];
  columns = columns.concat([
    {
      title: '操作',
      dataIndex: 'option',
      valueType: 'option',
      width: 120,
      render: (_, record) => {
        let cmps = [];

          cmps.push(
            <a
              key="edit"
              onClick={(e) => {
                setFormVisible(true);
                setCurrent(record);
              }}
            >
              编辑
            </a>,
          );

          cmps.push(
            <Popconfirm
              key="delete"
              title="确定要删除吗?"
              onConfirm={async () => {
                await handleRemove(record.id);
              }}
            >
              <a>删除</a>
            </Popconfirm>,
          );

        return cmps;
      },
    },
  ]);

  return (
    <DrawerForm<API.Geoip>
      open={open}
      width={1000}
      title={`IP地区分组管理`}
      drawerProps={{
        // forceRender: true,
        // destroyOnClose: true,
        // closable: false,
        onClose: onCancel,
        maskClosable: true,
      }}
      submitter={{
        render: (props, defaultDoms) => {
          return [
            <Button
              key="create"
              onClick={() => {
                onCancel();
              }}
            >
              关闭
            </Button>];
        },
      }}
    >

      <ProTable
        rowKey="id"
        options={{
          fullScreen: false,
          reload: false,
          setting: false,
          density: false,
        }}
        columnEmptyText={''}
        formRef={formRef}
        actionRef={actionRef}
        columns={columns}
        search={false}
        pagination={false}
        request={api.list}
        toolBarRender={() => {
          var toolbarDoms: any[] = [];

          toolbarDoms.push([
            <Button
              type="primary"
              key="create"
              onClick={() => {
                setFormVisible(true);
                setCurrent(undefined);
              }}
            >
              <PlusOutlined />
              新建
            </Button>
          ]);

          return toolbarDoms;
        }}
      />



      
      <FormAreaForm
        open={formVisible}
        onSubmit={handleSubmit}
        current={current}
        onCancel={() => {
          setFormVisible(false);
          setCurrent(undefined);
        }}
      />

    </DrawerForm>
  );
};

export default Form;
